<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN"
  "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">

<html xmlns="http://www.w3.org/1999/xhtml">
  <head>
    <meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
    
    <title>3. An Informal Introduction to Python Python 概要介绍 &mdash; Python v2.7 documentation</title>
    <link rel="stylesheet" href="../_static/default.css" type="text/css" />
    <link rel="stylesheet" href="../_static/pygments.css" type="text/css" />
    <script type="text/javascript">
      var DOCUMENTATION_OPTIONS = {
        URL_ROOT:    '../',
        VERSION:     '2.7',
        COLLAPSE_MODINDEX: false,
        FILE_SUFFIX: '.html',
        HAS_SOURCE:  true
      };
    </script>
    <script type="text/javascript" src="../_static/jquery.js"></script>
    <script type="text/javascript" src="../_static/doctools.js"></script>
    <link rel="search" type="application/opensearchdescription+xml"
          title="Search within Python v2.7 documentation"
          href="../_static/opensearch.xml"/>
    <link rel="author" title="About these documents" href="../about.html" />
    <link rel="copyright" title="Copyright" href="../copyright.html" />
    <link rel="top" title="Python v2.7 documentation" href="../index.html" />
    <link rel="up" title="The Python Tutorial" href="index.html" />
    <link rel="next" title="4. More Control Flow Tools 深入流程控制" href="controlflow.html" />
    <link rel="prev" title="2. Using the Python Interpreter 使用 Python 解释器" href="interpreter.html" />
    <link rel="shortcut icon" type="image/png" href="../_static/py.png" />
 

  </head>
  <body>
    <div class="related">
      <h3>Navigation</h3>
      <ul>
        <li class="right" style="margin-right: 10px">
          <a href="../genindex.html" title="General Index"
             accesskey="I">index</a></li>
        <li class="right" >
          <a href="../modindex.html" title="Global Module Index"
             accesskey="M">modules</a> |</li>
        <li class="right" >
          <a href="controlflow.html" title="4. More Control Flow Tools 深入流程控制"
             accesskey="N">next</a> |</li>
        <li class="right" >
          <a href="interpreter.html" title="2. Using the Python Interpreter 使用 Python 解释器"
             accesskey="P">previous</a> |</li>
        <li><img src="../_static/py.png" alt=""
                 style="vertical-align: middle; margin-top: -1px"/></li>
        <li><a href="../index.html">Python v2.7 documentation</a> &raquo;</li>

          <li><a href="index.html" accesskey="U">The Python Tutorial</a> &raquo;</li> 
      </ul>
    </div>  

    <div class="document">
      <div class="documentwrapper">
        <div class="bodywrapper">
          <div class="body">
            
  <div class="section" id="an-informal-introduction-to-python-python">
<span id="tut-informal"></span><h1>3. An Informal Introduction to Python Python 概要介绍<a class="headerlink" href="#an-informal-introduction-to-python-python" title="Permalink to this headline">¶</a></h1>
<p>In the following examples, input and output are distinguished by the presence or
absence of prompts (<tt class="docutils literal"><span class="pre">&gt;&gt;&gt;</span></tt> and <tt class="docutils literal"><span class="pre">...</span></tt>): to repeat the example, you must type
everything after the prompt, when the prompt appears; lines that do not begin
with a prompt are output from the interpreter. Note that a secondary prompt on a
line by itself in an example means you must type a blank line; this is used to
end a multi-line command.</p>
<p>下面的例子中，输入和输出分别由大于号和句号提示符（ <tt class="docutils literal"><span class="pre">&gt;&gt;&gt;</span></tt> 和
<tt class="docutils literal"><span class="pre">`...`</span></tt> ）标注。如果想重现这些例子，就要在解释器的提示符后，输入（提示
符后面的）那些不包含提示符的代码行。需要注意的是在练习中遇到的从属提示符表示
你需要在最后多输入一个空行，解释器才能知道这是一个多行命令的结束。</p>
<p>Many of the examples in this manual, even those entered at the interactive
prompt, include comments.  Comments in Python start with the hash character,
<tt class="docutils literal"><span class="pre">#</span></tt>, and extend to the end of the physical line.  A comment may appear at the
start of a line or following whitespace or code, but not within a string
literal.  A hash character within a string literal is just a hash character.
Since comments are to clarify code and are not interpreted by Python, they may
be omitted when typing in examples.</p>
<p>本手册中的很多示例——包括那些带有交互提示符的——都含有注释。Python 中的
注释以 # 字符起始，直至实际的行尾（译注——这里原作者用了
<tt class="docutils literal"><span class="pre">physical</span> <span class="pre">line</span></tt> 以表示实际的换行而非编辑器的自动换行）。注释可以从行
首开始，也可以在空白或代码之后，但是不出现在字符串中。文本字符串中的 #
字符仅仅表示 # 。代码中的注释不会被 Python 解释，录入示例的时候可以忽
略它们。</p>
<p>Some examples:
如下示例</p>
<div class="highlight-python"><div class="highlight"><pre><span class="c"># this is the first comment</span>
<span class="n">SPAM</span> <span class="o">=</span> <span class="mi">1</span>                 <span class="c"># and this is the second comment</span>
                         <span class="c"># ... and now a third!</span>
<span class="n">STRING</span> <span class="o">=</span> <span class="s">&quot;# This is not a comment.&quot;</span>
</pre></div>
</div>
<div class="section" id="using-python-as-a-calculator-python">
<span id="tut-calculator"></span><h2>3.1. Using Python as a Calculator 将 Python 当做计算器<a class="headerlink" href="#using-python-as-a-calculator-python" title="Permalink to this headline">¶</a></h2>
<p>Let&#8217;s try some simple Python commands.  Start the interpreter and wait for the
primary prompt, <tt class="docutils literal"><span class="pre">&gt;&gt;&gt;</span></tt>.  (It shouldn&#8217;t take long.)</p>
<p>我们来尝试一些简单的 Python 命令。启动解释器然后等待主提示符 <tt class="docutils literal"><span class="pre">&gt;&gt;&gt;</span></tt>
出现（不需要很久）。</p>
<div class="section" id="numbers">
<span id="tut-numbers"></span><h3>3.1.1. Numbers 数值<a class="headerlink" href="#numbers" title="Permalink to this headline">¶</a></h3>
<p>The interpreter acts as a simple calculator: you can type an expression at it
and it will write the value.  Expression syntax is straightforward: the
operators <tt class="docutils literal"><span class="pre">+</span></tt>, <tt class="docutils literal"><span class="pre">-</span></tt>, <tt class="docutils literal"><span class="pre">*</span></tt> and <tt class="docutils literal"><span class="pre">/</span></tt> work just like in most other languages
(for example, Pascal or C); parentheses can be used for grouping.  For example:</p>
<p>解释器的表示就像一个简单的计算器：可以向其录入一些表达式，它会给出返回
值。表达式语法很直白：运算符 <tt class="docutils literal"><span class="pre">+</span></tt> ， <tt class="docutils literal"><span class="pre">-</span></tt> ， <tt class="docutils literal"><span class="pre">*</span></tt> 和 <tt class="docutils literal"><span class="pre">/</span></tt> 与其它语
言一样（例如： Pascal 或 C）；括号用于分组。例如</p>
<div class="highlight-python"><div class="highlight"><pre><span class="gp">&gt;&gt;&gt; </span><span class="mi">2</span><span class="o">+</span><span class="mi">2</span>
<span class="go">4</span>
<span class="gp">&gt;&gt;&gt; </span><span class="c"># This is a comment</span>
<span class="gp">... </span><span class="mi">2</span><span class="o">+</span><span class="mi">2</span>
<span class="go">4</span>
<span class="gp">&gt;&gt;&gt; </span><span class="mi">2</span><span class="o">+</span><span class="mi">2</span>  <span class="c"># and a comment on the same line as code</span>
<span class="go">4</span>
<span class="gp">&gt;&gt;&gt; </span><span class="p">(</span><span class="mi">50</span><span class="o">-</span><span class="mi">5</span><span class="o">*</span><span class="mi">6</span><span class="p">)</span><span class="o">/</span><span class="mi">4</span>
<span class="go">5</span>
<span class="gp">&gt;&gt;&gt; </span><span class="c"># Integer division returns the floor:</span>
<span class="gp">... </span><span class="mi">7</span><span class="o">/</span><span class="mi">3</span>
<span class="go">2</span>
<span class="gp">&gt;&gt;&gt; </span><span class="mi">7</span><span class="o">/-</span><span class="mi">3</span>
<span class="go">-3</span>
</pre></div>
</div>
<p>The equal sign (<tt class="docutils literal"><span class="pre">'='</span></tt>) is used to assign a value to a variable. Afterwards, no
result is displayed before the next interactive prompt:</p>
<p>等号（ <tt class="docutils literal"><span class="pre">'='</span></tt> ）用于给变量赋值</p>
<div class="highlight-python"><div class="highlight"><pre><span class="gp">&gt;&gt;&gt; </span><span class="n">width</span> <span class="o">=</span> <span class="mi">20</span>
<span class="gp">&gt;&gt;&gt; </span><span class="n">height</span> <span class="o">=</span> <span class="mi">5</span><span class="o">*</span><span class="mi">9</span>
<span class="gp">&gt;&gt;&gt; </span><span class="n">width</span> <span class="o">*</span> <span class="n">height</span>
<span class="go">900</span>
</pre></div>
</div>
<p>A value can be assigned to several variables simultaneously:</p>
<p>一个值可以同时赋给几个变量</p>
<div class="highlight-python"><div class="highlight"><pre><span class="gp">&gt;&gt;&gt; </span><span class="n">x</span> <span class="o">=</span> <span class="n">y</span> <span class="o">=</span> <span class="n">z</span> <span class="o">=</span> <span class="mi">0</span>  <span class="c"># Zero x, y and z</span>
<span class="gp">&gt;&gt;&gt; </span><span class="n">x</span>
<span class="go">0</span>
<span class="gp">&gt;&gt;&gt; </span><span class="n">y</span>
<span class="go">0</span>
<span class="gp">&gt;&gt;&gt; </span><span class="n">z</span>
<span class="go">0</span>
</pre></div>
</div>
<p>Variables must be &#8220;defined&#8221; (assigned a value) before they can be used, or an
error will occur:</p>
<p>变量在使用前必须“定义”（赋值），否则会出错</p>
<div class="highlight-python"><div class="highlight"><pre><span class="gp">&gt;&gt;&gt; </span><span class="c"># try to access an undefined variable</span>
<span class="gp">... </span><span class="n">n</span>
<span class="gt">Traceback (most recent call last):</span>
  File <span class="nb">&quot;&lt;stdin&gt;&quot;</span>, line <span class="m">1</span>, in <span class="n-Identifier">&lt;module&gt;</span>
<span class="nc">NameError</span>: <span class="n-Identifier">name &#39;n&#39; is not defined</span>
</pre></div>
</div>
<p>There is full support for floating point; operators with mixed type operands
convert the integer operand to floating point:</p>
<p>浮点数有完整的支持；与整型混合计算时会自动转为浮点数</p>
<div class="highlight-python"><div class="highlight"><pre><span class="gp">&gt;&gt;&gt; </span><span class="mi">3</span> <span class="o">*</span> <span class="mf">3.75</span> <span class="o">/</span> <span class="mf">1.5</span>
<span class="go">7.5</span>
<span class="gp">&gt;&gt;&gt; </span><span class="mf">7.0</span> <span class="o">/</span> <span class="mi">2</span>
<span class="go">3.5</span>
</pre></div>
</div>
<p>Complex numbers are also supported; imaginary numbers are written with a suffix
of <tt class="docutils literal"><span class="pre">j</span></tt> or <tt class="docutils literal"><span class="pre">J</span></tt>.  Complex numbers with a nonzero real component are written as
<tt class="docutils literal"><span class="pre">(real+imagj)</span></tt>, or can be created with the <tt class="docutils literal"><span class="pre">complex(real,</span> <span class="pre">imag)</span></tt> function.
:</p>
<p>复数也得到支持；带有后缀 <tt class="docutils literal"><span class="pre">j</span></tt> 或 <tt class="docutils literal"><span class="pre">J</span></tt> 就被视为虚数。带有非零实部的复
数写为 <tt class="docutils literal"><span class="pre">(real+imagj)</span></tt> ，或者可以用 <tt class="docutils literal"><span class="pre">complex(real,</span> <span class="pre">imag)</span></tt> 函数创建</p>
<div class="highlight-python"><div class="highlight"><pre><span class="gp">&gt;&gt;&gt; </span><span class="mi">1</span><span class="n">j</span> <span class="o">*</span> <span class="mi">1</span><span class="n">J</span>
<span class="go">(-1+0j)</span>
<span class="gp">&gt;&gt;&gt; </span><span class="mi">1</span><span class="n">j</span> <span class="o">*</span> <span class="nb">complex</span><span class="p">(</span><span class="mi">0</span><span class="p">,</span><span class="mi">1</span><span class="p">)</span>
<span class="go">(-1+0j)</span>
<span class="gp">&gt;&gt;&gt; </span><span class="mi">3</span><span class="o">+</span><span class="mi">1</span><span class="n">j</span><span class="o">*</span><span class="mi">3</span>
<span class="go">(3+3j)</span>
<span class="gp">&gt;&gt;&gt; </span><span class="p">(</span><span class="mi">3</span><span class="o">+</span><span class="mi">1</span><span class="n">j</span><span class="p">)</span><span class="o">*</span><span class="mi">3</span>
<span class="go">(9+3j)</span>
<span class="gp">&gt;&gt;&gt; </span><span class="p">(</span><span class="mi">1</span><span class="o">+</span><span class="mi">2</span><span class="n">j</span><span class="p">)</span><span class="o">/</span><span class="p">(</span><span class="mi">1</span><span class="o">+</span><span class="mi">1</span><span class="n">j</span><span class="p">)</span>
<span class="go">(1.5+0.5j)</span>
</pre></div>
</div>
<p>Complex numbers are always represented as two floating point numbers, the real
and imaginary part.  To extract these parts from a complex number <em>z</em>, use
<tt class="docutils literal"><span class="pre">z.real</span></tt> and <tt class="docutils literal"><span class="pre">z.imag</span></tt>. :</p>
<p>复数的实部和虚部总是记为两个浮点数。要从复数 <em>z</em> 中提取实部和虚部，使
用 <tt class="docutils literal"><span class="pre">z.real</span></tt> 和 <tt class="docutils literal"><span class="pre">z.imag</span></tt> 。</p>
<div class="highlight-python"><div class="highlight"><pre><span class="gp">&gt;&gt;&gt; </span><span class="n">a</span><span class="o">=</span><span class="mf">1.5</span><span class="o">+</span><span class="mf">0.5</span><span class="n">j</span>
<span class="gp">&gt;&gt;&gt; </span><span class="n">a</span><span class="o">.</span><span class="n">real</span>
<span class="go">1.5</span>
<span class="gp">&gt;&gt;&gt; </span><span class="n">a</span><span class="o">.</span><span class="n">imag</span>
<span class="go">0.5</span>
</pre></div>
</div>
<p>The conversion functions to floating point and integer (<a title="float" class="reference external" href="../library/functions.html#float"><tt class="xref docutils literal"><span class="pre">float()</span></tt></a>,
<a title="int" class="reference external" href="../library/functions.html#int"><tt class="xref docutils literal"><span class="pre">int()</span></tt></a> and <a title="long" class="reference external" href="../library/functions.html#long"><tt class="xref docutils literal"><span class="pre">long()</span></tt></a>) don&#8217;t work for complex numbers &#8212; there is no one
correct way to convert a complex number to a real number.  Use <tt class="docutils literal"><span class="pre">abs(z)</span></tt> to get
its magnitude (as a float) or <tt class="docutils literal"><span class="pre">z.real</span></tt> to get its real part. :</p>
<p>浮点数和整数之间的转换函数（ <a title="float" class="reference external" href="../library/functions.html#float"><tt class="xref docutils literal"><span class="pre">float()</span></tt></a> 和 <a title="int" class="reference external" href="../library/functions.html#int"><tt class="xref docutils literal"><span class="pre">int()</span></tt></a> 以及
<a title="long" class="reference external" href="../library/functions.html#long"><tt class="xref docutils literal"><span class="pre">long()</span></tt></a> ） 不能用于复数。没有什么正确方法可以把一个复数转成一个实
数。函数 <tt class="docutils literal"><span class="pre">abs(z)</span></tt> 用于获取其模（浮点数）或 <tt class="docutils literal"><span class="pre">z.real</span></tt> 获取其实部</p>
<div class="highlight-python"><div class="highlight"><pre><span class="gp">&gt;&gt;&gt; </span><span class="n">a</span><span class="o">=</span><span class="mf">3.0</span><span class="o">+</span><span class="mf">4.0</span><span class="n">j</span>
<span class="gp">&gt;&gt;&gt; </span><span class="nb">float</span><span class="p">(</span><span class="n">a</span><span class="p">)</span>
<span class="gt">Traceback (most recent call last):</span>
  File <span class="nb">&quot;&lt;stdin&gt;&quot;</span>, line <span class="m">1</span>, in <span class="n-Identifier">?</span>
<span class="nc">TypeError</span>: <span class="n-Identifier">can&#39;t convert complex to float; use abs(z)</span>
<span class="gp">&gt;&gt;&gt; </span><span class="n">a</span><span class="o">.</span><span class="n">real</span>
<span class="go">3.0</span>
<span class="gp">&gt;&gt;&gt; </span><span class="n">a</span><span class="o">.</span><span class="n">imag</span>
<span class="go">4.0</span>
<span class="gp">&gt;&gt;&gt; </span><span class="nb">abs</span><span class="p">(</span><span class="n">a</span><span class="p">)</span>  <span class="c"># sqrt(a.real**2 + a.imag**2)</span>
<span class="go">5.0</span>
</pre></div>
</div>
<p>In interactive mode, the last printed expression is assigned to the variable
<tt class="docutils literal"><span class="pre">_</span></tt>.  This means that when you are using Python as a desk calculator, it is
somewhat easier to continue calculations, for example:</p>
<p>交互模式中，最近一个表达式的值赋给变量 <tt class="docutils literal"><span class="pre">_</span></tt> 。这样我们就可以把它当作
一个桌面计算器，很方便的用于连续计算，例如</p>
<div class="highlight-python"><div class="highlight"><pre><span class="gp">&gt;&gt;&gt; </span><span class="n">tax</span> <span class="o">=</span> <span class="mf">12.5</span> <span class="o">/</span> <span class="mi">100</span>
<span class="gp">&gt;&gt;&gt; </span><span class="n">price</span> <span class="o">=</span> <span class="mf">100.50</span>
<span class="gp">&gt;&gt;&gt; </span><span class="n">price</span> <span class="o">*</span> <span class="n">tax</span>
<span class="go">12.5625</span>
<span class="gp">&gt;&gt;&gt; </span><span class="n">price</span> <span class="o">+</span> <span class="n">_</span>
<span class="go">113.0625</span>
<span class="gp">&gt;&gt;&gt; </span><span class="nb">round</span><span class="p">(</span><span class="n">_</span><span class="p">,</span> <span class="mi">2</span><span class="p">)</span>
<span class="go">113.06</span>
</pre></div>
</div>
<p>This variable should be treated as read-only by the user.  Don&#8217;t explicitly
assign a value to it &#8212; you would create an independent local variable with the
same name masking the built-in variable with its magic behavior.</p>
<p>此变量对于用户是只读的。不要尝试给它赋值 —— 你只会创建一个独立的同名局
部变量，它屏蔽了系统内置变量的魔术效果。</p>
</div>
<div class="section" id="strings">
<span id="tut-strings"></span><h3>3.1.2. Strings 字符串<a class="headerlink" href="#strings" title="Permalink to this headline">¶</a></h3>
<p>Besides numbers, Python can also manipulate strings, which can be expressed in
several ways.  They can be enclosed in single quotes or double quotes:</p>
<p>相比数值，Python 也提供了可以通过几种不同方式传递的字符串。它们可以用
单引号或双引号标识</p>
<div class="highlight-python"><div class="highlight"><pre><span class="gp">&gt;&gt;&gt; </span><span class="s">&#39;spam eggs&#39;</span>
<span class="go">&#39;spam eggs&#39;</span>
<span class="gp">&gt;&gt;&gt; </span><span class="s">&#39;doesn</span><span class="se">\&#39;</span><span class="s">t&#39;</span>
<span class="go">&quot;doesn&#39;t&quot;</span>
<span class="gp">&gt;&gt;&gt; </span><span class="s">&quot;doesn&#39;t&quot;</span>
<span class="go">&quot;doesn&#39;t&quot;</span>
<span class="gp">&gt;&gt;&gt; </span><span class="s">&#39;&quot;Yes,&quot; he said.&#39;</span>
<span class="go">&#39;&quot;Yes,&quot; he said.&#39;</span>
<span class="gp">&gt;&gt;&gt; </span><span class="s">&quot;</span><span class="se">\&quot;</span><span class="s">Yes,</span><span class="se">\&quot;</span><span class="s"> he said.&quot;</span>
<span class="go">&#39;&quot;Yes,&quot; he said.&#39;</span>
<span class="gp">&gt;&gt;&gt; </span><span class="s">&#39;&quot;Isn</span><span class="se">\&#39;</span><span class="s">t,&quot; she said.&#39;</span>
<span class="go">&#39;&quot;Isn\&#39;t,&quot; she said.&#39;</span>
</pre></div>
</div>
<p>String literals can span multiple lines in several ways.  Continuation lines can
be used, with a backslash as the last character on the line indicating that the
next line is a logical continuation of the line:</p>
<p>字符串文本有几种方法分行。可以使用反斜杠为行结尾的连续字符串，它表示下
一行在逻辑上是本行的后续内容</p>
<div class="highlight-python"><div class="highlight"><pre><span class="n">hello</span> <span class="o">=</span> <span class="s">&quot;This is a rather long string containing</span><span class="se">\n\</span>
<span class="s">several lines of text just as you would do in C.</span><span class="se">\n\</span>
<span class="s">    Note that whitespace at the beginning of the line is</span><span class="se">\</span>
<span class="s"> significant.&quot;</span>

<span class="k">print</span> <span class="n">hello</span>
</pre></div>
</div>
<p>Note that newlines still need to be embedded in the string using <tt class="docutils literal"><span class="pre">\n</span></tt> &#8211; the
newline following the trailing backslash is discarded.  This example would print
the following:</p>
<p>需要注意的是，还是需要在字符串中写入 <tt class="docutils literal"><span class="pre">\n</span></tt> ——结尾的反斜杠会被忽略。前
例会打印为如下形式：</p>
<div class="highlight-text"><div class="highlight"><pre>This is a rather long string containing
several lines of text just as you would do in C.
    Note that whitespace at the beginning of the line is significant.
</pre></div>
</div>
<p>Or, strings can be surrounded in a pair of matching triple-quotes: <tt class="docutils literal"><span class="pre">&quot;&quot;&quot;</span></tt> or
<tt class="docutils literal"><span class="pre">'''</span></tt>.  End of lines do not need to be escaped when using triple-quotes, but
they will be included in the string. :</p>
<p>另外，字符串可以标识在一对儿三引号中： <tt class="docutils literal"><span class="pre">&quot;&quot;&quot;</span></tt> 或 <tt class="docutils literal"><span class="pre">'''</span></tt> 。三引号中，
不需要行属转义，它们已经包含在字符串中</p>
<div class="highlight-python"><div class="highlight"><pre><span class="k">print</span> <span class="s">&quot;&quot;&quot;</span>
<span class="s">Usage: thingy [OPTIONS]</span>
<span class="s">     -h                        Display this usage message</span>
<span class="s">     -H hostname               Hostname to connect to</span>
<span class="s">&quot;&quot;&quot;</span>
</pre></div>
</div>
<p>produces the following output:</p>
<p>得到以下输出</p>
<div class="highlight-python"><pre>.. code-block:: text</pre>
</div>
<blockquote>
<dl class="docutils">
<dt>Usage: thingy [OPTIONS]</dt>
<dd><table class="first last docutils option-list" frame="void" rules="none">
<col class="option" />
<col class="description" />
<tbody valign="top">
<tr><td class="option-group">
<kbd><span class="option">-h</span></kbd></td>
<td>Display this usage message</td></tr>
<tr><td class="option-group">
<kbd><span class="option">-H <var>hostname</var></span></kbd></td>
<td>Hostname to connect to</td></tr>
</tbody>
</table>
</dd>
</dl>
</blockquote>
<p>If we make the string literal a &#8220;raw&#8221; string, <tt class="docutils literal"><span class="pre">\n</span></tt> sequences are not converted
to newlines, but the backslash at the end of the line, and the newline character
in the source, are both included in the string as data.  Thus, the example:</p>
<p>如果我们生成一个“原始”字符串， <tt class="docutils literal"><span class="pre">\n</span></tt> 序列不会被转义，而且行尾的反斜
杠，源码中的换行符，都成为字符串中的一部分数据，因此下例</p>
<div class="highlight-python"><div class="highlight"><pre><span class="n">hello</span> <span class="o">=</span> <span class="s">r&quot;This is a rather long string containing\n</span><span class="se">\</span>
<span class="s">several lines of text much as you would do in C.&quot;</span>

<span class="k">print</span> <span class="n">hello</span>
</pre></div>
</div>
<p>would print:</p>
<p>会打印：</p>
<div class="highlight-text"><div class="highlight"><pre>This is a rather long string containing\n\
several lines of text much as you would do in C.
</pre></div>
</div>
<p>The interpreter prints the result of string operations in the same way as they
are typed for input: inside quotes, and with quotes and other funny characters
escaped by backslashes, to show the precise value.  The string is enclosed in
double quotes if the string contains a single quote and no double quotes, else
it&#8217;s enclosed in single quotes.  (The <a class="reference external" href="../reference/simple_stmts.html#print"><tt class="xref docutils literal"><span class="pre">print</span></tt></a> statement, described
later, can be used to write strings without quotes or escapes.)</p>
<p>解释器打印的字符串操作结果与它们输入时的方式一致：以括号标识，包含反斜
杠转义的有趣的字符，以精确的显示值。如果字符串包含单引号，不包含双引
号，它就以双引号标识。否则它以单引号标识。（后面介绍的
<a class="reference external" href="../reference/simple_stmts.html#print"><tt class="xref docutils literal"><span class="pre">print</span></tt></a> 语句，可以输出没有标识和转义的字符串。）</p>
<p>Strings can be concatenated (glued together) with the <tt class="docutils literal"><span class="pre">+</span></tt> operator, and
repeated with <tt class="docutils literal"><span class="pre">*</span></tt>:</p>
<p>字符串可以由 <tt class="docutils literal"><span class="pre">+</span></tt> 操作符连接（粘到一起），可以由 <tt class="docutils literal"><span class="pre">*</span></tt> 重复</p>
<div class="highlight-python"><div class="highlight"><pre><span class="gp">&gt;&gt;&gt; </span><span class="n">word</span> <span class="o">=</span> <span class="s">&#39;Help&#39;</span> <span class="o">+</span> <span class="s">&#39;A&#39;</span>
<span class="gp">&gt;&gt;&gt; </span><span class="n">word</span>
<span class="go">&#39;HelpA&#39;</span>
<span class="gp">&gt;&gt;&gt; </span><span class="s">&#39;&lt;&#39;</span> <span class="o">+</span> <span class="n">word</span><span class="o">*</span><span class="mi">5</span> <span class="o">+</span> <span class="s">&#39;&gt;&#39;</span>
<span class="go">&#39;&lt;HelpAHelpAHelpAHelpAHelpA&gt;&#39;</span>
</pre></div>
</div>
<p>Two string literals next to each other are automatically concatenated; the first
line above could also have been written <tt class="docutils literal"><span class="pre">word</span> <span class="pre">=</span> <span class="pre">'Help'</span> <span class="pre">'A'</span></tt>; this only works
with two literals, not with arbitrary string expressions:</p>
<p>相邻的两个字符串文本自动连接在一起，前面那行代码也可以写为
<tt class="docutils literal"><span class="pre">word</span> <span class="pre">='Help'</span> <span class="pre">'A'</span></tt> ，它只用于两个字符串文本，不能用于字符串表达式。</p>
<div class="highlight-python"><div class="highlight"><pre><span class="gp">&gt;&gt;&gt; </span><span class="s">&#39;str&#39;</span> <span class="s">&#39;ing&#39;</span>                   <span class="c">#  &lt;-  This is ok</span>
<span class="go">&#39;string&#39;</span>
<span class="gp">&gt;&gt;&gt; </span><span class="s">&#39;str&#39;</span><span class="o">.</span><span class="n">strip</span><span class="p">()</span> <span class="o">+</span> <span class="s">&#39;ing&#39;</span>   <span class="c">#  &lt;-  This is ok</span>
<span class="go">&#39;string&#39;</span>
<span class="gp">&gt;&gt;&gt; </span><span class="s">&#39;str&#39;</span><span class="o">.</span><span class="n">strip</span><span class="p">()</span> <span class="s">&#39;ing&#39;</span>     <span class="c">#  &lt;-  This is invalid</span>
<span class="go">  File &quot;&lt;stdin&gt;&quot;, line 1, in ?</span>
<span class="go">    &#39;str&#39;.strip() &#39;ing&#39;</span>
<span class="go">                      ^</span>
<span class="go">SyntaxError: invalid syntax</span>
</pre></div>
</div>
<p>Strings can be subscripted (indexed); like in C, the first character of a string
has subscript (index) 0.  There is no separate character type; a character is
simply a string of size one.  Like in Icon, substrings can be specified with the
<em>slice notation</em>: two indices separated by a colon. :</p>
<p>字符串也可以被截取（检索）。类似于 C ，字符串的第一个字符索引为 0 。没
有独立的字符类型，字符就是长度为 1 的字符串。类似 Icon ，可以用
<em>切片标注</em> 法截取字符串：由两个索引分割的复本。</p>
<div class="highlight-python"><div class="highlight"><pre><span class="gp">&gt;&gt;&gt; </span><span class="n">word</span><span class="p">[</span><span class="mi">4</span><span class="p">]</span>
<span class="go">&#39;A&#39;</span>
<span class="gp">&gt;&gt;&gt; </span><span class="n">word</span><span class="p">[</span><span class="mi">0</span><span class="p">:</span><span class="mi">2</span><span class="p">]</span>
<span class="go">&#39;He&#39;</span>
<span class="gp">&gt;&gt;&gt; </span><span class="n">word</span><span class="p">[</span><span class="mi">2</span><span class="p">:</span><span class="mi">4</span><span class="p">]</span>
<span class="go">&#39;lp&#39;</span>
</pre></div>
</div>
<p>Slice indices have useful defaults; an omitted first index defaults to zero, an
omitted second index defaults to the size of the string being sliced. :</p>
<p>索引切片可以有默认值，切片时，忽略第一个索引的话，默认为0，忽略第二个
索引，默认为字符串的长度。（其实还有第三个参数，表示切片步长，它默认为
1，完整的切片操作是 word[2:4:1] ——译者）</p>
<div class="highlight-python"><div class="highlight"><pre><span class="gp">&gt;&gt;&gt; </span><span class="n">word</span><span class="p">[:</span><span class="mi">2</span><span class="p">]</span>    <span class="c"># The first two characters</span>
<span class="go">&#39;He&#39;</span>
<span class="gp">&gt;&gt;&gt; </span><span class="n">word</span><span class="p">[</span><span class="mi">2</span><span class="p">:]</span>    <span class="c"># Everything except the first two characters</span>
<span class="go">&#39;lpA&#39;</span>
</pre></div>
</div>
<p>Unlike a C string, Python strings cannot be changed.  Assigning to an indexed
position in the string results in an error:</p>
<p>不同于 C 字符串，Python 字符串不可变。向字符串文本的某一个索引赋值会引
发错误 :</p>
<div class="highlight-python"><div class="highlight"><pre><span class="gp">&gt;&gt;&gt; </span><span class="n">word</span><span class="p">[</span><span class="mi">0</span><span class="p">]</span> <span class="o">=</span> <span class="s">&#39;x&#39;</span>
<span class="gt">Traceback (most recent call last):</span>
  File <span class="nb">&quot;&lt;stdin&gt;&quot;</span>, line <span class="m">1</span>, in <span class="n-Identifier">?</span>
<span class="nc">TypeError</span>: <span class="n-Identifier">object does not support item assignment</span>
<span class="gp">&gt;&gt;&gt; </span><span class="n">word</span><span class="p">[:</span><span class="mi">1</span><span class="p">]</span> <span class="o">=</span> <span class="s">&#39;Splat&#39;</span>
<span class="gt">Traceback (most recent call last):</span>
  File <span class="nb">&quot;&lt;stdin&gt;&quot;</span>, line <span class="m">1</span>, in <span class="n-Identifier">?</span>
<span class="nc">TypeError</span>: <span class="n-Identifier">object does not support slice assignment</span>
</pre></div>
</div>
<p>However, creating a new string with the combined content is easy and efficient:</p>
<p>不过，组合文本内容生成一个新文本简单而高效 :</p>
<div class="highlight-python"><div class="highlight"><pre><span class="gp">&gt;&gt;&gt; </span><span class="s">&#39;x&#39;</span> <span class="o">+</span> <span class="n">word</span><span class="p">[</span><span class="mi">1</span><span class="p">:]</span>
<span class="go">&#39;xelpA&#39;</span>
<span class="gp">&gt;&gt;&gt; </span><span class="s">&#39;Splat&#39;</span> <span class="o">+</span> <span class="n">word</span><span class="p">[</span><span class="mi">4</span><span class="p">]</span>
<span class="go">&#39;SplatA&#39;</span>
</pre></div>
</div>
<p>Here&#8217;s a useful invariant of slice operations: <tt class="docutils literal"><span class="pre">s[:i]</span> <span class="pre">+</span> <span class="pre">s[i:]</span></tt> equals <tt class="docutils literal"><span class="pre">s</span></tt>.
:</p>
<p>切片操作有个有用的不变性： <tt class="docutils literal"><span class="pre">s[:i]</span> <span class="pre">+</span> <span class="pre">s[i:]</span></tt> 等于 <tt class="docutils literal"><span class="pre">s</span></tt> 。</p>
<div class="highlight-python"><div class="highlight"><pre><span class="gp">&gt;&gt;&gt; </span><span class="n">word</span><span class="p">[:</span><span class="mi">2</span><span class="p">]</span> <span class="o">+</span> <span class="n">word</span><span class="p">[</span><span class="mi">2</span><span class="p">:]</span>
<span class="go">&#39;HelpA&#39;</span>
<span class="gp">&gt;&gt;&gt; </span><span class="n">word</span><span class="p">[:</span><span class="mi">3</span><span class="p">]</span> <span class="o">+</span> <span class="n">word</span><span class="p">[</span><span class="mi">3</span><span class="p">:]</span>
<span class="go">&#39;HelpA&#39;</span>
</pre></div>
</div>
<p>Degenerate slice indices are handled gracefully: an index that is too large is
replaced by the string size, an upper bound smaller than the lower bound returns
an empty string. :</p>
<p>退化的切割检索很优雅：上限过大，会替换为文本长度，上界小于下界则返回空
字符串。</p>
<div class="highlight-python"><div class="highlight"><pre><span class="gp">&gt;&gt;&gt; </span><span class="n">word</span><span class="p">[</span><span class="mi">1</span><span class="p">:</span><span class="mi">100</span><span class="p">]</span>
<span class="go">&#39;elpA&#39;</span>
<span class="gp">&gt;&gt;&gt; </span><span class="n">word</span><span class="p">[</span><span class="mi">10</span><span class="p">:]</span>
<span class="go">&#39;&#39;</span>
<span class="gp">&gt;&gt;&gt; </span><span class="n">word</span><span class="p">[</span><span class="mi">2</span><span class="p">:</span><span class="mi">1</span><span class="p">]</span>
<span class="go">&#39;&#39;</span>
</pre></div>
</div>
<p>Indices may be negative numbers, to start counting from the right. For example:</p>
<p>索引可以是负数，此时从右端开始计量。例如 :</p>
<div class="highlight-python"><div class="highlight"><pre><span class="gp">&gt;&gt;&gt; </span><span class="n">word</span><span class="p">[</span><span class="o">-</span><span class="mi">1</span><span class="p">]</span>     <span class="c"># The last character</span>
<span class="go">&#39;A&#39;</span>
<span class="gp">&gt;&gt;&gt; </span><span class="n">word</span><span class="p">[</span><span class="o">-</span><span class="mi">2</span><span class="p">]</span>     <span class="c"># The last-but-one character</span>
<span class="go">&#39;p&#39;</span>
<span class="gp">&gt;&gt;&gt; </span><span class="n">word</span><span class="p">[</span><span class="o">-</span><span class="mi">2</span><span class="p">:]</span>    <span class="c"># The last two characters</span>
<span class="go">&#39;pA&#39;</span>
<span class="gp">&gt;&gt;&gt; </span><span class="n">word</span><span class="p">[:</span><span class="o">-</span><span class="mi">2</span><span class="p">]</span>    <span class="c"># Everything except the last two characters</span>
<span class="go">&#39;Hel&#39;</span>
</pre></div>
</div>
<p>But note that -0 is really the same as 0, so it does not count from the right!
:</p>
<p>不过需要注意的是 -0 实际上就是 0，所以它不会从右边开始计数！</p>
<div class="highlight-python"><div class="highlight"><pre><span class="gp">&gt;&gt;&gt; </span><span class="n">word</span><span class="p">[</span><span class="o">-</span><span class="mi">0</span><span class="p">]</span>     <span class="c"># (since -0 equals 0)</span>
<span class="go">&#39;H&#39;</span>
</pre></div>
</div>
<p>Out-of-range negative slice indices are truncated, but don&#8217;t try this for
single-element (non-slice) indices:</p>
<p>负索引切片越界会被截断，不要尝试将它用于单元素（非切片）检索</p>
<div class="highlight-python"><div class="highlight"><pre><span class="gp">&gt;&gt;&gt; </span><span class="n">word</span><span class="p">[</span><span class="o">-</span><span class="mi">100</span><span class="p">:]</span>
<span class="go">&#39;HelpA&#39;</span>
<span class="gp">&gt;&gt;&gt; </span><span class="n">word</span><span class="p">[</span><span class="o">-</span><span class="mi">10</span><span class="p">]</span>    <span class="c"># error</span>
<span class="gt">Traceback (most recent call last):</span>
  File <span class="nb">&quot;&lt;stdin&gt;&quot;</span>, line <span class="m">1</span>, in <span class="n-Identifier">?</span>
<span class="nc">IndexError</span>: <span class="n-Identifier">string index out of range</span>
</pre></div>
</div>
<p>One way to remember how slices work is to think of the indices as pointing
<em>between</em> characters, with the left edge of the first character numbered 0.
Then the right edge of the last character of a string of <em>n</em> characters has
index <em>n</em>, for example:</p>
<p>有个办法可以很容易的记住切片的工作方式：切片时的索引是在两个字符
<em>之间</em> 。左边第一个字符的索引为0，，而长度为 <em>n</em> 的字符串其最后一个字
符的右界索引为 <em>n</em> 。例如</p>
<div class="highlight-python"><pre> +---+---+---+---+---+
 | H | e | l | p | A |
 +---+---+---+---+---+
 0   1   2   3   4   5
-5  -4  -3  -2  -1</pre>
</div>
<p>The first row of numbers gives the position of the indices 0...5 in the string;
the second row gives the corresponding negative indices. The slice from <em>i</em> to
<em>j</em> consists of all characters between the edges labeled <em>i</em> and <em>j</em>,
respectively.</p>
<p>文本中的第一行数字给出字符串中的索引点 0...5 。第二行给出相应的负索引。
切片是从 <em>i</em> 到 <em>j</em> 两个数值标示的边界之间的所有字符。</p>
<p>For non-negative indices, the length of a slice is the difference of the
indices, if both are within bounds.  For example, the length of <tt class="docutils literal"><span class="pre">word[1:3]</span></tt> is
2.</p>
<p>对于非负索引，如果上下都在边界内，切片长度与索引不同。例如，
<tt class="docutils literal"><span class="pre">word[1:3]</span></tt> 是 2 。</p>
<p>The built-in function <a title="len" class="reference external" href="../library/functions.html#len"><tt class="xref docutils literal"><span class="pre">len()</span></tt></a> returns the length of a string:</p>
<p>内置函数 <a title="len" class="reference external" href="../library/functions.html#len"><tt class="xref docutils literal"><span class="pre">len()</span></tt></a> 返回字符串长度</p>
<div class="highlight-python"><div class="highlight"><pre><span class="gp">&gt;&gt;&gt; </span><span class="n">s</span> <span class="o">=</span> <span class="s">&#39;supercalifragilisticexpialidocious&#39;</span>
<span class="gp">&gt;&gt;&gt; </span><span class="nb">len</span><span class="p">(</span><span class="n">s</span><span class="p">)</span>
<span class="go">34</span>
</pre></div>
</div>
<div class="admonition-see-also admonition seealso">
<p class="first admonition-title">See also</p>
<dl class="last docutils">
<dt><a class="reference external" href="../library/stdtypes.html#typesseq"><em>Sequence Types &#8212; str, unicode, list, tuple, buffer, xrange</em></a></dt>
<dd>Strings, and the Unicode strings described in the next section, are
examples of <em>sequence types</em>, and support the common operations supported
by such types.</dd>
<dt><a class="reference external" href="../library/stdtypes.html#string-methods"><em>String Methods</em></a></dt>
<dd>Both strings and Unicode strings support a large number of methods for
basic transformations and searching.</dd>
<dt><a class="reference external" href="../library/string.html#new-string-formatting"><em>String Formatting</em></a></dt>
<dd>Information about string formatting with <a title="str.format" class="reference external" href="../library/stdtypes.html#str.format"><tt class="xref docutils literal"><span class="pre">str.format()</span></tt></a> is described
here.</dd>
<dt><a class="reference external" href="../library/stdtypes.html#string-formatting"><em>String Formatting Operations</em></a></dt>
<dd>The old formatting operations invoked when strings and Unicode strings are
the left operand of the <tt class="docutils literal"><span class="pre">%</span></tt> operator are described in more detail here.</dd>
</dl>
</div>
</div>
<div class="section" id="unicode-strings-unicode">
<span id="tut-unicodestrings"></span><h3>3.1.3. Unicode Strings Unicode 文本<a class="headerlink" href="#unicode-strings-unicode" title="Permalink to this headline">¶</a></h3>
<p>Starting with Python 2.0 a new data type for storing text data is available to
the programmer: the Unicode object. It can be used to store and manipulate
Unicode data (see <a class="reference external" href="http://www.unicode.org/">http://www.unicode.org/</a>) and integrates well with the existing
string objects, providing auto-conversions where necessary.</p>
<p>从 Python 2.0 起，程序员们有了一个新的，用来存储文本数据的类型： Unicode
对象。它可以用于存储和维护 Unicode 数据（参见
<a class="reference external" href="http://www.unicode.org/">http://www.unicode.org/</a> ） ，并且与现有的字符串对象有良好的集成，必要
时提供自动转换。</p>
<p>Unicode has the advantage of providing one ordinal for every character in every
script used in modern and ancient texts. Previously, there were only 256
possible ordinals for script characters. Texts were typically bound to a code
page which mapped the ordinals to script characters. This lead to very much
confusion especially with respect to internationalization (usually written as
<tt class="docutils literal"><span class="pre">i18n</span></tt> &#8212; <tt class="docutils literal"><span class="pre">'i'</span></tt> + 18 characters + <tt class="docutils literal"><span class="pre">'n'</span></tt>) of software.  Unicode solves
these problems by defining one code page for all scripts.</p>
<p>Unicode 的先进之处在于为每一种现代或古代使用的文字系统中出现的每一个字符都
提供了统一的序列号。之前，文字系统中的字符只能有 256 种可能的顺序。通过代
码页分界映射。文本绑定到映射文字系统的代码页。这在软件国际化的时候尤其
麻烦 （通常写作 <tt class="docutils literal"><span class="pre">i18n</span></tt> —— <tt class="docutils literal"><span class="pre">'i'</span></tt> + 18 个字符 + <tt class="docutils literal"><span class="pre">'n'</span></tt> ）。 Unicode
解决了为所有的文字系统设置一个独立代码页的难题。</p>
<p>Creating Unicode strings in Python is just as simple as creating normal
strings:</p>
<p>在 Python 中创建 Unicode 字符串和创建普通的字符串一样简单</p>
<div class="highlight-python"><div class="highlight"><pre><span class="gp">&gt;&gt;&gt; </span><span class="s">u&#39;Hello World !&#39;</span>
<span class="go">u&#39;Hello World !&#39;</span>
</pre></div>
</div>
<p>The small <tt class="docutils literal"><span class="pre">'u'</span></tt> in front of the quote indicates that a Unicode string is
supposed to be created. If you want to include special characters in the string,
you can do so by using the Python <em>Unicode-Escape</em> encoding. The following
example shows how:</p>
<p>引号前的 <tt class="docutils literal"><span class="pre">'u'</span></tt> 表示这会创建一个 Unicode 字符串。如果想要在字符串中包
含特殊字符，可以使用 Python 的 <em>Unicode-Escape</em> （Unicode 转义——译者）。
请看下面的例子</p>
<div class="highlight-python"><div class="highlight"><pre><span class="gp">&gt;&gt;&gt; </span><span class="s">u&#39;Hello</span><span class="se">\u0020</span><span class="s">World !&#39;</span>
<span class="go">u&#39;Hello World !&#39;</span>
</pre></div>
</div>
<p>For experts, there is also a raw mode just like the one for normal strings. You
have to prefix the opening quote with &#8216;ur&#8217; to have unevenPython use the
<em>Raw-Unicode-Escape</em> encoding. It will only apply the above <tt class="docutils literal"><span class="pre">\uXXXX</span></tt>
conversion if there is an uneven number of backslashes in front of the small
&#8216;u&#8217;. :</p>
<p>特别的，和普通字符串一样， Unicode 字符串也有原始模式。可以在引号前加
“ur”，Python 会采用 <em>Raw-Unicode-Escape</em> 编码（原始 Unicode 转义——译
者）。如果有前缀为 &#8216;u&#8217; 的数值，它也只会显示为 <tt class="docutils literal"><span class="pre">\uXXXX</span></tt> 。</p>
<div class="highlight-python"><div class="highlight"><pre><span class="gp">&gt;&gt;&gt; </span><span class="s">ur&#39;Hello\u0020World !&#39;</span>
<span class="go">u&#39;Hello World !&#39;</span>
<span class="gp">&gt;&gt;&gt; </span><span class="s">ur&#39;Hello</span><span class="se">\\</span><span class="s">u0020World !&#39;</span>
<span class="go">u&#39;Hello\\\\u0020World !&#39;</span>
</pre></div>
</div>
<p>The raw mode is most useful when you have to enter lots of backslashes, as can
be necessary in regular expressions.</p>
<p>如果你需要大量输入反斜杠，原始模式非常有用，这在正则表达式中几乎是必须
的。</p>
<p>Apart from these standard encodings, Python provides a whole set of other ways
of creating Unicode strings on the basis of a known encoding.</p>
<p>所为这些编码标准的一部分，Python 提供了基于已知编码来创建 Unicode 字符
串的整套方法。</p>
<p id="index-1462">The built-in function <a title="unicode" class="reference external" href="../library/functions.html#unicode"><tt class="xref docutils literal"><span class="pre">unicode()</span></tt></a> provides access to all registered Unicode
codecs (COders and DECoders). Some of the more well known encodings which these
codecs can convert are <em>Latin-1</em>, <em>ASCII</em>, <em>UTF-8</em>, and <em>UTF-16</em>. The latter two
are variable-length encodings that store each Unicode character in one or more
bytes. The default encoding is normally set to ASCII, which passes through
characters in the range 0 to 127 and rejects any other characters with an error.
When a Unicode string is printed, written to a file, or converted with
<a title="str" class="reference external" href="../library/functions.html#str"><tt class="xref docutils literal"><span class="pre">str()</span></tt></a>, conversion takes place using this default encoding. :</p>
<p>内置函数 <a title="unicode" class="reference external" href="../library/functions.html#unicode"><tt class="xref docutils literal"><span class="pre">unicode()</span></tt></a> 可以使用所有注册的 Unicode 编码（ COders 和
DECoders ）。众所周知， <em>Latin-1</em> ， <em>ASCII</em> ， <em>UTF-8</em> 和 <em>UTF-16</em> 之
类的编码可以互相转换（Latin-1 表示一个很小的拉丁语言符号集，与 ASCII 基
本一致，其实不能用来表示庞大的东方语言字符集——译者）。后两个是变长编
码，将每一个 Uniocde 字符存储为一到多个字节。默认通常编码为 ASCII，此
编码接受 0 到 127 这个范围的编码，否则报错。将一个 Unicode 字符串打印
或写入到文件中，或者使用 <a title="str" class="reference external" href="../library/functions.html#str"><tt class="xref docutils literal"><span class="pre">str()</span></tt></a> 转换时，转换操作以此为默认编码。</p>
<div class="highlight-python"><div class="highlight"><pre><span class="gp">&gt;&gt;&gt; </span><span class="s">u&quot;abc&quot;</span>
<span class="go">u&#39;abc&#39;</span>
<span class="gp">&gt;&gt;&gt; </span><span class="nb">str</span><span class="p">(</span><span class="s">u&quot;abc&quot;</span><span class="p">)</span>
<span class="go">&#39;abc&#39;</span>
<span class="gp">&gt;&gt;&gt; </span><span class="s">u&quot;äöü&quot;</span>
<span class="go">u&#39;\xe4\xf6\xfc&#39;</span>
<span class="gp">&gt;&gt;&gt; </span><span class="nb">str</span><span class="p">(</span><span class="s">u&quot;äöü&quot;</span><span class="p">)</span>
<span class="gt">Traceback (most recent call last):</span>
  File <span class="nb">&quot;&lt;stdin&gt;&quot;</span>, line <span class="m">1</span>, in <span class="n-Identifier">?</span>
<span class="nc">UnicodeEncodeError: &#39;ascii&#39; codec can&#39;t encode characters in position 0-2</span>: <span class="n-Identifier">ordinal not in range(128)</span>
</pre></div>
</div>
<p>To convert a Unicode string into an 8-bit string using a specific encoding,
Unicode objects provide an <tt class="xref docutils literal"><span class="pre">encode()</span></tt> method that takes one argument, the
name of the encoding.  Lowercase names for encodings are preferred. :</p>
<p>为了将一个 Unicode 字符串写为一个使用特定编码的 8 位字符串， Unicode 对象提供一
<tt class="xref docutils literal"><span class="pre">encode()</span></tt> 方法，它接受编码名作为参数。编码名应该小写。</p>
<div class="highlight-python"><div class="highlight"><pre><span class="gp">&gt;&gt;&gt; </span><span class="s">u&quot;äöü&quot;</span><span class="o">.</span><span class="n">encode</span><span class="p">(</span><span class="s">&#39;utf-8&#39;</span><span class="p">)</span>
<span class="go">&#39;\xc3\xa4\xc3\xb6\xc3\xbc&#39;</span>
</pre></div>
</div>
<p>If you have data in a specific encoding and want to produce a corresponding
Unicode string from it, you can use the <a title="unicode" class="reference external" href="../library/functions.html#unicode"><tt class="xref docutils literal"><span class="pre">unicode()</span></tt></a> function with the
encoding name as the second argument. :</p>
<p>如果有一个其它编码的数据，希望可以从中生成一 Unicode 字符串，你可以使用 <a title="unicode" class="reference external" href="../library/functions.html#unicode"><tt class="xref docutils literal"><span class="pre">unicode()</span></tt></a>
函数，它接受编码名作为第二参数。</p>
<div class="highlight-python"><div class="highlight"><pre><span class="gp">&gt;&gt;&gt; </span><span class="nb">unicode</span><span class="p">(</span><span class="s">&#39;</span><span class="se">\xc3\xa4\xc3\xb6\xc3\xbc</span><span class="s">&#39;</span><span class="p">,</span> <span class="s">&#39;utf-8&#39;</span><span class="p">)</span>
<span class="go">u&#39;\xe4\xf6\xfc&#39;</span>
</pre></div>
</div>
</div>
<div class="section" id="lists">
<span id="tut-lists"></span><h3>3.1.4. Lists 列表<a class="headerlink" href="#lists" title="Permalink to this headline">¶</a></h3>
<p>Python knows a number of <em>compound</em> data types, used to group together other
values.  The most versatile is the <em>list</em>, which can be written as a list of
comma-separated values (items) between square brackets.  List items need not all
have the same type. :</p>
<p>Python 有几个 <em>复合</em> 数据类型，用于分线其它的值。最通用的是 <em>list</em> (列
表) ，它可以写作中括号之间的一列逗号分隔的值。列表的元素不必是同一类型。</p>
<div class="highlight-python"><div class="highlight"><pre><span class="gp">&gt;&gt;&gt; </span><span class="n">a</span> <span class="o">=</span> <span class="p">[</span><span class="s">&#39;spam&#39;</span><span class="p">,</span> <span class="s">&#39;eggs&#39;</span><span class="p">,</span> <span class="mi">100</span><span class="p">,</span> <span class="mi">1234</span><span class="p">]</span>
<span class="gp">&gt;&gt;&gt; </span><span class="n">a</span>
<span class="go">[&#39;spam&#39;, &#39;eggs&#39;, 100, 1234]</span>
</pre></div>
</div>
<p>Like string indices, list indices start at 0, and lists can be sliced,
concatenated and so on:</p>
<p>就像字符串索引，列表从 0 开始检索。列表可以被切片和连接</p>
<div class="highlight-python"><div class="highlight"><pre><span class="gp">&gt;&gt;&gt; </span><span class="n">a</span><span class="p">[</span><span class="mi">0</span><span class="p">]</span>
<span class="go">&#39;spam&#39;</span>
<span class="gp">&gt;&gt;&gt; </span><span class="n">a</span><span class="p">[</span><span class="mi">3</span><span class="p">]</span>
<span class="go">1234</span>
<span class="gp">&gt;&gt;&gt; </span><span class="n">a</span><span class="p">[</span><span class="o">-</span><span class="mi">2</span><span class="p">]</span>
<span class="go">100</span>
<span class="gp">&gt;&gt;&gt; </span><span class="n">a</span><span class="p">[</span><span class="mi">1</span><span class="p">:</span><span class="o">-</span><span class="mi">1</span><span class="p">]</span>
<span class="go">[&#39;eggs&#39;, 100]</span>
<span class="gp">&gt;&gt;&gt; </span><span class="n">a</span><span class="p">[:</span><span class="mi">2</span><span class="p">]</span> <span class="o">+</span> <span class="p">[</span><span class="s">&#39;bacon&#39;</span><span class="p">,</span> <span class="mi">2</span><span class="o">*</span><span class="mi">2</span><span class="p">]</span>
<span class="go">[&#39;spam&#39;, &#39;eggs&#39;, &#39;bacon&#39;, 4]</span>
<span class="gp">&gt;&gt;&gt; </span><span class="mi">3</span><span class="o">*</span><span class="n">a</span><span class="p">[:</span><span class="mi">3</span><span class="p">]</span> <span class="o">+</span> <span class="p">[</span><span class="s">&#39;Boo!&#39;</span><span class="p">]</span>
<span class="go">[&#39;spam&#39;, &#39;eggs&#39;, 100, &#39;spam&#39;, &#39;eggs&#39;, 100, &#39;spam&#39;, &#39;eggs&#39;, 100, &#39;Boo!&#39;]</span>
</pre></div>
</div>
<p>All slice operations return a new list containing the requested elements.  This
means that the following slice returns a shallow copy of the list <em>a</em>:</p>
<p>所有的切片操作都会返回新的列表，包含求得的元素。这意味着以下的切片操作
返回列表 <em>a</em> 的一个浅复制副本 :</p>
<div class="highlight-python"><div class="highlight"><pre><span class="gp">&gt;&gt;&gt; </span><span class="n">a</span><span class="p">[:]</span>
<span class="go">[&#39;spam&#39;, &#39;eggs&#39;, 100, 1234]</span>
</pre></div>
</div>
<p>Unlike strings, which are <em>immutable</em>, it is possible to change individual
elements of a list:</p>
<p>不像 <em>不可变的</em> 字符串，列表允许修改元素</p>
<div class="highlight-python"><div class="highlight"><pre><span class="gp">&gt;&gt;&gt; </span><span class="n">a</span>
<span class="go">[&#39;spam&#39;, &#39;eggs&#39;, 100, 1234]</span>
<span class="gp">&gt;&gt;&gt; </span><span class="n">a</span><span class="p">[</span><span class="mi">2</span><span class="p">]</span> <span class="o">=</span> <span class="n">a</span><span class="p">[</span><span class="mi">2</span><span class="p">]</span> <span class="o">+</span> <span class="mi">23</span>
<span class="gp">&gt;&gt;&gt; </span><span class="n">a</span>
<span class="go">[&#39;spam&#39;, &#39;eggs&#39;, 123, 1234]</span>
</pre></div>
</div>
<p>Assignment to slices is also possible, and this can even change the size of the
list or clear it entirely:</p>
<p>也可以对切片赋值，此操作可以改变列表的尺寸，或清空它</p>
<div class="highlight-python"><div class="highlight"><pre><span class="gp">&gt;&gt;&gt; </span><span class="c"># Replace some items:</span>
<span class="gp">... </span><span class="n">a</span><span class="p">[</span><span class="mi">0</span><span class="p">:</span><span class="mi">2</span><span class="p">]</span> <span class="o">=</span> <span class="p">[</span><span class="mi">1</span><span class="p">,</span> <span class="mi">12</span><span class="p">]</span>
<span class="gp">&gt;&gt;&gt; </span><span class="n">a</span>
<span class="go">[1, 12, 123, 1234]</span>
<span class="gp">&gt;&gt;&gt; </span><span class="c"># Remove some:</span>
<span class="gp">... </span><span class="n">a</span><span class="p">[</span><span class="mi">0</span><span class="p">:</span><span class="mi">2</span><span class="p">]</span> <span class="o">=</span> <span class="p">[]</span>
<span class="gp">&gt;&gt;&gt; </span><span class="n">a</span>
<span class="go">[123, 1234]</span>
<span class="gp">&gt;&gt;&gt; </span><span class="c"># Insert some:</span>
<span class="gp">... </span><span class="n">a</span><span class="p">[</span><span class="mi">1</span><span class="p">:</span><span class="mi">1</span><span class="p">]</span> <span class="o">=</span> <span class="p">[</span><span class="s">&#39;bletch&#39;</span><span class="p">,</span> <span class="s">&#39;xyzzy&#39;</span><span class="p">]</span>
<span class="gp">&gt;&gt;&gt; </span><span class="n">a</span>
<span class="go">[123, &#39;bletch&#39;, &#39;xyzzy&#39;, 1234]</span>
<span class="gp">&gt;&gt;&gt; </span><span class="c"># Insert (a copy of) itself at the beginning</span>
<span class="gp">&gt;&gt;&gt; </span><span class="n">a</span><span class="p">[:</span><span class="mi">0</span><span class="p">]</span> <span class="o">=</span> <span class="n">a</span>
<span class="gp">&gt;&gt;&gt; </span><span class="n">a</span>
<span class="go">[123, &#39;bletch&#39;, &#39;xyzzy&#39;, 1234, 123, &#39;bletch&#39;, &#39;xyzzy&#39;, 1234]</span>
<span class="gp">&gt;&gt;&gt; </span><span class="c"># Clear the list: replace all items with an empty list</span>
<span class="gp">&gt;&gt;&gt; </span><span class="n">a</span><span class="p">[:]</span> <span class="o">=</span> <span class="p">[]</span>
<span class="gp">&gt;&gt;&gt; </span><span class="n">a</span>
<span class="go">[]</span>
</pre></div>
</div>
<p>The built-in function <a title="len" class="reference external" href="../library/functions.html#len"><tt class="xref docutils literal"><span class="pre">len()</span></tt></a> also applies to lists:</p>
<p>内置函数 <a title="len" class="reference external" href="../library/functions.html#len"><tt class="xref docutils literal"><span class="pre">len()</span></tt></a> 也可以用于列表</p>
<div class="highlight-python"><div class="highlight"><pre><span class="gp">&gt;&gt;&gt; </span><span class="n">a</span> <span class="o">=</span> <span class="p">[</span><span class="s">&#39;a&#39;</span><span class="p">,</span> <span class="s">&#39;b&#39;</span><span class="p">,</span> <span class="s">&#39;c&#39;</span><span class="p">,</span> <span class="s">&#39;d&#39;</span><span class="p">]</span>
<span class="gp">&gt;&gt;&gt; </span><span class="nb">len</span><span class="p">(</span><span class="n">a</span><span class="p">)</span>
<span class="go">4</span>
</pre></div>
</div>
<p>It is possible to nest lists (create lists containing other lists), for
example:</p>
<p>允许嵌套列表（创建一个包含其它列表的列表），例如</p>
<div class="highlight-python"><div class="highlight"><pre><span class="gp">&gt;&gt;&gt; </span><span class="n">q</span> <span class="o">=</span> <span class="p">[</span><span class="mi">2</span><span class="p">,</span> <span class="mi">3</span><span class="p">]</span>
<span class="gp">&gt;&gt;&gt; </span><span class="n">p</span> <span class="o">=</span> <span class="p">[</span><span class="mi">1</span><span class="p">,</span> <span class="n">q</span><span class="p">,</span> <span class="mi">4</span><span class="p">]</span>
<span class="gp">&gt;&gt;&gt; </span><span class="nb">len</span><span class="p">(</span><span class="n">p</span><span class="p">)</span>
<span class="go">3</span>
<span class="gp">&gt;&gt;&gt; </span><span class="n">p</span><span class="p">[</span><span class="mi">1</span><span class="p">]</span>
<span class="go">[2, 3]</span>
<span class="gp">&gt;&gt;&gt; </span><span class="n">p</span><span class="p">[</span><span class="mi">1</span><span class="p">][</span><span class="mi">0</span><span class="p">]</span>
<span class="go">2</span>
<span class="gp">&gt;&gt;&gt; </span><span class="n">p</span><span class="p">[</span><span class="mi">1</span><span class="p">]</span><span class="o">.</span><span class="n">append</span><span class="p">(</span><span class="s">&#39;xtra&#39;</span><span class="p">)</span>     <span class="c"># See section 5.1</span>
<span class="gp">&gt;&gt;&gt; </span><span class="n">p</span>
<span class="go">[1, [2, 3, &#39;xtra&#39;], 4]</span>
<span class="gp">&gt;&gt;&gt; </span><span class="n">q</span>
<span class="go">[2, 3, &#39;xtra&#39;]</span>
</pre></div>
</div>
<p>Note that in the last example, <tt class="docutils literal"><span class="pre">p[1]</span></tt> and <tt class="docutils literal"><span class="pre">q</span></tt> really refer to the same
object!  We&#8217;ll come back to <em>object semantics</em> later.</p>
<p>注意最后一个例子中， <tt class="docutils literal"><span class="pre">p[1]</span></tt> 和 <tt class="docutils literal"><span class="pre">q</span></tt> 实际上指向同一个对象！ 我们会在
后面的 <em>object semantics</em> 中继续讨论。</p>
</div>
</div>
<div class="section" id="first-steps-towards-programming">
<span id="tut-firststeps"></span><h2>3.2. First Steps Towards Programming 编程的第一步<a class="headerlink" href="#first-steps-towards-programming" title="Permalink to this headline">¶</a></h2>
<p>Of course, we can use Python for more complicated tasks than adding two and two
together.  For instance, we can write an initial sub-sequence of the <em>Fibonacci</em>
series as follows:</p>
<p>当然，我们可以使用 Python 完成比二加二更复杂的任务。例如，我们可以写一
个生成 <em>菲波那契</em> 子序列的程序，如下所示</p>
<div class="highlight-python"><div class="highlight"><pre><span class="gp">&gt;&gt;&gt; </span><span class="c"># Fibonacci series:</span>
<span class="gp">... </span><span class="c"># the sum of two elements defines the next</span>
<span class="gp">... </span><span class="n">a</span><span class="p">,</span> <span class="n">b</span> <span class="o">=</span> <span class="mi">0</span><span class="p">,</span> <span class="mi">1</span>
<span class="gp">&gt;&gt;&gt; </span><span class="k">while</span> <span class="n">b</span> <span class="o">&lt;</span> <span class="mi">10</span><span class="p">:</span>
<span class="gp">... </span>    <span class="k">print</span> <span class="n">b</span>
<span class="gp">... </span>    <span class="n">a</span><span class="p">,</span> <span class="n">b</span> <span class="o">=</span> <span class="n">b</span><span class="p">,</span> <span class="n">a</span><span class="o">+</span><span class="n">b</span>
<span class="gp">...</span>
<span class="go">1</span>
<span class="go">1</span>
<span class="go">2</span>
<span class="go">3</span>
<span class="go">5</span>
<span class="go">8</span>
</pre></div>
</div>
<p>This example introduces several new features.</p>
<p>这个例子介绍了几个新功能。</p>
<ul>
<li><p class="first">The first line contains a <em>multiple assignment</em>: the variables <tt class="docutils literal"><span class="pre">a</span></tt> and <tt class="docutils literal"><span class="pre">b</span></tt>
simultaneously get the new values 0 and 1.  On the last line this is used again,
demonstrating that the expressions on the right-hand side are all evaluated
first before any of the assignments take place.  The right-hand side expressions
are evaluated  from the left to the right.</p>
<p>第一行包括了一个 <em>多重赋值</em> ：变量 <tt class="docutils literal"><span class="pre">a</span></tt> 和 <tt class="docutils literal"><span class="pre">b</span></tt> 同时获得了新的值 0
和 1 。最后一行又使用了一次。在这个演示中，变量赋值前，右边首先完成
计算。右边的表达式从左到右计算。</p>
</li>
<li><p class="first">The <a class="reference external" href="../reference/compound_stmts.html#while"><tt class="xref docutils literal"><span class="pre">while</span></tt></a> loop executes as long as the condition (here: <tt class="docutils literal"><span class="pre">b</span> <span class="pre">&lt;</span> <span class="pre">10</span></tt>)
remains true.  In Python, like in C, any non-zero integer value is true; zero is
false.  The condition may also be a string or list value, in fact any sequence;
anything with a non-zero length is true, empty sequences are false.  The test
used in the example is a simple comparison.  The standard comparison operators
are written the same as in C: <tt class="docutils literal"><span class="pre">&lt;</span></tt> (less than), <tt class="docutils literal"><span class="pre">&gt;</span></tt> (greater than), <tt class="docutils literal"><span class="pre">==</span></tt>
(equal to), <tt class="docutils literal"><span class="pre">&lt;=</span></tt> (less than or equal to), <tt class="docutils literal"><span class="pre">&gt;=</span></tt> (greater than or equal to)
and <tt class="docutils literal"><span class="pre">!=</span></tt> (not equal to).</p>
<p>条件（这里是 <tt class="docutils literal"><span class="pre">b</span> <span class="pre">&lt;</span> <span class="pre">10</span></tt> ）为 true 时， <a class="reference external" href="../reference/compound_stmts.html#while"><tt class="xref docutils literal"><span class="pre">while</span></tt></a> 循环执行。在
Python 中，类似于 C ，任何非零整数都是 true；0 是 false 。条件也可以
是字符串或列表，实际上可以是任何序列；所有长度不为零的是 true ，空序
列是 false。示例中的测试是一个简单的比较。标准比较操作符与 C 相同：
<tt class="docutils literal"><span class="pre">&lt;</span></tt> （小于）， <tt class="docutils literal"><span class="pre">&gt;</span></tt> （大于）， <tt class="docutils literal"><span class="pre">==</span></tt> （等于）， <tt class="docutils literal"><span class="pre">&lt;=</span></tt> （小于等
于）， <tt class="docutils literal"><span class="pre">&gt;=</span></tt>  （大于等于）和 <tt class="docutils literal"><span class="pre">!=</span></tt> （不等于）。</p>
</li>
<li><p class="first">The <em>body</em> of the loop is <em>indented</em>: indentation is Python&#8217;s way of grouping
statements.  Python does not (yet!) provide an intelligent input line editing
facility, so you have to type a tab or space(s) for each indented line.  In
practice you will prepare more complicated input for Python with a text editor;
most text editors have an auto-indent facility.  When a compound statement is
entered interactively, it must be followed by a blank line to indicate
completion (since the parser cannot guess when you have typed the last line).
Note that each line within a basic block must be indented by the same amount.</p>
<p>循环 <em>体</em> 是 <em>缩进</em> 的：缩进是 Python 是 Python 组织語句的方法。
Python (还) 不提供集成的行编辑功能，所以你要为每一个缩进行输入 TAB
或空格。实践中建议你找个文本编辑来录入复杂的 Python 程序，大多数文本
编辑器提供自动缩进。交互式录入复合语句时，必须在最后输入一个空行来标
识结束（因为解释器没办法猜测你输入的哪一行是最后一行），需要注意的是
同一个语句块中的语句块必须缩进同样数量的空白。</p>
</li>
<li><p class="first">The <a class="reference external" href="../reference/simple_stmts.html#print"><tt class="xref docutils literal"><span class="pre">print</span></tt></a> statement writes the value of the expression(s) it is
given.  It differs from just writing the expression you want to write (as we did
earlier in the calculator examples) in the way it handles multiple expressions
and strings.  Strings are printed without quotes, and a space is inserted
between items, so you can format things nicely, like this:</p>
<p>关键字 <a class="reference external" href="../reference/simple_stmts.html#print"><tt class="xref docutils literal"><span class="pre">print</span></tt></a> 语句输出给定表达式的值。它控制多个表达式和字
符串输出为你想要字符串（就像我们在前面计算器的例子中那样）。字符串打
印时不用引号包围，每两个子项之间插入空间，所以你可以把格式弄得很漂
亮，像这样</p>
<div class="highlight-python"><div class="highlight"><pre><span class="gp">&gt;&gt;&gt; </span><span class="n">i</span> <span class="o">=</span> <span class="mi">256</span><span class="o">*</span><span class="mi">256</span>
<span class="gp">&gt;&gt;&gt; </span><span class="k">print</span> <span class="s">&#39;The value of i is&#39;</span><span class="p">,</span> <span class="n">i</span>
<span class="go">The value of i is 65536</span>
</pre></div>
</div>
<p>A trailing comma avoids the newline after the output:</p>
<p>用一个逗号结尾就可以禁止输出换行</p>
<div class="highlight-python"><div class="highlight"><pre><span class="gp">&gt;&gt;&gt; </span><span class="n">a</span><span class="p">,</span> <span class="n">b</span> <span class="o">=</span> <span class="mi">0</span><span class="p">,</span> <span class="mi">1</span>
<span class="gp">&gt;&gt;&gt; </span><span class="k">while</span> <span class="n">b</span> <span class="o">&lt;</span> <span class="mi">1000</span><span class="p">:</span>
<span class="gp">... </span>    <span class="k">print</span> <span class="n">b</span><span class="p">,</span>
<span class="gp">... </span>    <span class="n">a</span><span class="p">,</span> <span class="n">b</span> <span class="o">=</span> <span class="n">b</span><span class="p">,</span> <span class="n">a</span><span class="o">+</span><span class="n">b</span>
<span class="gp">...</span>
<span class="go">1 1 2 3 5 8 13 21 34 55 89 144 233 377 610 987</span>
</pre></div>
</div>
<p>Note that the interpreter inserts a newline before it prints the next prompt if
the last line was not completed.</p>
<p>注意，在这里，如果最后一行没有输出完全，解释器在它打印下一个提示符前
会插入一个换行。</p>
</li>
</ul>
</div>
</div>


          </div>
        </div>
      </div>
      <div class="sphinxsidebar">
        <div class="sphinxsidebarwrapper">
            <h3><a href="../contents.html">Table Of Contents</a></h3>
            <ul>
<li><a class="reference external" href="#">3. An Informal Introduction to Python Python 概要介绍</a><ul>
<li><a class="reference external" href="#using-python-as-a-calculator-python">3.1. Using Python as a Calculator 将 Python 当做计算器</a><ul>
<li><a class="reference external" href="#numbers">3.1.1. Numbers 数值</a></li>
<li><a class="reference external" href="#strings">3.1.2. Strings 字符串</a></li>
<li><a class="reference external" href="#unicode-strings-unicode">3.1.3. Unicode Strings Unicode 文本</a></li>
<li><a class="reference external" href="#lists">3.1.4. Lists 列表</a></li>
</ul>
</li>
<li><a class="reference external" href="#first-steps-towards-programming">3.2. First Steps Towards Programming 编程的第一步</a></li>
</ul>
</li>
</ul>

            <h4>Previous topic</h4>
            <p class="topless"><a href="interpreter.html"
                                  title="previous chapter">2. Using the Python Interpreter 使用　Python 解释器</a></p>
            <h4>Next topic</h4>
            <p class="topless"><a href="controlflow.html"
                                  title="next chapter">4. More Control Flow Tools 深入流程控制</a></p>
<h3>This Page</h3>
<ul class="this-page-menu">
  <li><a href="../bugs.html">Report a Bug</a></li>
  <li><a href="../_sources/tutorial/introduction.txt"
         rel="nofollow">Show Source</a></li>
</ul>

          <div id="searchbox" style="display: none">
            <h3>Quick search</h3>
              <form class="search" action="../search.html" method="get">
                <input type="text" name="q" size="18" />
                <input type="submit" value="Go" />
                <input type="hidden" name="check_keywords" value="yes" />
                <input type="hidden" name="area" value="default" />
              </form>
              <p class="searchtip" style="font-size: 90%">
              Enter search terms or a module, class or function name.
              </p>
          </div>
          <script type="text/javascript">$('#searchbox').show(0);</script>
        </div>
      </div>
      <div class="clearer"></div>
    </div>
    <div class="related">
      <h3>Navigation</h3>
      <ul>
        <li class="right" style="margin-right: 10px">
          <a href="../genindex.html" title="General Index"
             >index</a></li>
        <li class="right" >
          <a href="../modindex.html" title="Global Module Index"
             >modules</a> |</li>
        <li class="right" >
          <a href="controlflow.html" title="4. More Control Flow Tools 深入流程控制"
             >next</a> |</li>
        <li class="right" >
          <a href="interpreter.html" title="2. Using the Python Interpreter 使用 Python 解释器"
             >previous</a> |</li>
        <li><img src="../_static/py.png" alt=""
                 style="vertical-align: middle; margin-top: -1px"/></li>
        <li><a href="../index.html">Python v2.7 documentation</a> &raquo;</li>

          <li><a href="index.html" >The Python Tutorial</a> &raquo;</li> 
      </ul>
    </div>
    <div class="footer">
    &copy; <a href="../copyright.html">Copyright</a> 1990-2010, Python Software Foundation.
    <br />
    The Python Software Foundation is a non-profit corporation.  
    <a href="http://www.python.org/psf/donations/">Please donate.</a>
    <br />
    Last updated on Oct 04, 2010.
    <a href="../bugs.html">Found a bug</a>?
    <br />
    Created using <a href="http://sphinx.pocoo.org/">Sphinx</a> 0.6.5.
    </div>

  </body>
</html>